/**    
 * 文件名：ReserveBillRequestHandler.java</br>
 *    
 * 版本信息：v1.0</br>
 * 日期：2017年7月12日</br>
 * © 2005-2017 雷技信息科技（上海）有限公司版权所有</br>
 *
 */
package com.logic.landseaserver.models.payment.impl;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.logic.landseaserver.common.enums.DepositEnum;
import com.logic.landseaserver.common.exception.LandseaException;
import com.logic.landseaserver.common.exception.ResultCodes.PaymentResultCode;
import com.logic.landseaserver.domain.BillPayment;
import com.logic.landseaserver.models.payment.PaymentConstant;
import com.logic.landseaserver.models.payment.PaymentConstant.BillTypeCode;
import com.logic.landseaserver.models.payment.bean.PaymentHelperBean;
import com.logic.landseaserver.models.payment.bean.PaymentRequest;
import com.logic.landseaserver.models.payment.service.AbstractPaymentRequestHandler;
import com.logic.landseaserver.persistence.read.DepositReadMapper;
import com.logic.landseaserver.persistence.write.BillPaymentWriteMapper;
import com.logic.landseaserver.ws.dto.DepositDTO;

/**
 *
 * 项目名称：system-server</br>
 * 类名称：ReserveBillRequestHandler</br>
 * 类描述：预订账单请求处理</br>
 * 创建人：Aaron</br>
 * 创建时间：2017年7月12日 下午4:47:49</br>
 * 
 * @version 1.0
 *
 */
@Service(BillTypeCode.RESERVE + PaymentConstant.PAYMENT_REQUEST_SUFFIX)
public class ReserveBillRequestHandler extends AbstractPaymentRequestHandler
{
    @Autowired
    private DepositReadMapper depositReadMapper;
    
    @Autowired
    private BillPaymentWriteMapper billPayWriteMapper;
    
    /**
     * [描述]：</br>
     *
     * @param payment
     * @param helperObject
     * @throws LandseaException
     *
     */
    @Override
    public void customCheckPayParam(PaymentRequest payment, PaymentHelperBean helperObject)
        throws LandseaException
    {
        // 预定金额不能修改，只能一次性支付，金额直接从预定单中获取，不需要从页面传入
    }
    
    /**
     * [描述]：业务逻辑检查</br>
     *
     * @param payment
     * @param helperObject
     * @throws LandseaException
     *
     */
    @Override
    public void checkPayBillAndMoney(PaymentRequest payment, PaymentHelperBean helperObject)
        throws LandseaException
    {
        String logInfo = getLog(payment, helperObject.getPaymentId());
        Integer billId = payment.getBillId();
        
        try
        {
            // 此处预定信息必须存在
            DepositDTO deposit = depositReadMapper.getDepositDetail(billId);
            if (null == deposit)
            {
                // 账单不存在
                LOGGER.error(logInfo + " deposit does not exists in ls_deposit table,depositId=" + billId);
                throw LandseaException.createException(PaymentResultCode.E00150002);
            }
            
            // 检查预定状态,只有草稿的预定才可以支付
            if(!DepositEnum._DS1.getCode().equals(deposit.getStatus()))
            {
                LOGGER.error(logInfo + " deposit status is not draft can not pay,depositId=" + billId);
                throw LandseaException.createException(PaymentResultCode.E00150029);
            }
            
            helperObject.setProjectId(deposit.getPrjId());
            
            // 把钱放进去
            payment.setMoney(deposit.getDepositAmount());
        }
        catch (LandseaException e)
        {
            throw e;
        }
        catch (Exception e)
        {
            LOGGER.error(logInfo + " checkPayBillAndMoney occur a error.", e);
            throw LandseaException.createException(PaymentResultCode.E00150019);
        }
    }
    
    /**
     * [描述]：</br>
     *
     * @param payment
     * @param helperObject
     * @throws LandseaException
     *
     */
    @Override
    public void doLocalBusiness(BillPayment billPayment, PaymentHelperBean helperObject, Map<String, Object> resultMap)
        throws LandseaException
    {
        billPayWriteMapper.insertSelective(billPayment);
        
        // 将paymentId重新赋值
        helperObject.setPaymentId(billPayment.getId());
        resultMap.put("paymentId", billPayment.getId());
    }
    
    /**
     * [描述]：</br>
     *
     * @param payment
     * @param helperObject
     * @return
     *
     */
    @Override
    public Integer getPaymentId(BillPayment billPayment, PaymentHelperBean helperObject)
    {
        return helperObject.getPaymentId();
    }
    
    /**
     * [描述]：</br>
     *
     * @return
     *
     */
    @Override
    public String getProcessClassName()
    {
        return this.getClass().getSimpleName();
    }
    
}
